﻿using Renci.SshNet;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace sshtool
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
             mPATH1 = ConfigurationManager.AppSettings["Path1"];
             mPATH2 = ConfigurationManager.AppSettings["Path2"];
            mtime = ConfigurationManager.AppSettings["mtime"];
            int t = Convert.ToInt32(mtime);
            
            lb1.Content = mPATH1;
            lb2.Content = mPATH2;
            lb3.Content = mtime;
            var timer = new Timer(state =>
            {
                TestTranferFiles();
            }, null, 0, t*000);

            //TestTranferFiles();
            //test1();
        }

        public string mPATH1 { get; private set; }
        public string mPATH2 { get; private set; }
        public string mtime { get; private set; }
        int k1 = 0;
        int k2 = 0;
        private void test1()
        {
            var connectionInfo = new ConnectionInfo("10.114.2.58",
                                        "mbl",
                                        new PasswordAuthenticationMethod("mbl", "Qq66669999")
                                        );
            using (var client = new SftpClient(connectionInfo))
            {
                client.Connect();
            }
        }

        private void TestTranferFiles()
        {
           
            try
            {
                
                string server = "10.114.2.58";
                string username = "mbl";
                string password = "Qq66669999";
                //  string command = Command.Text;
                var scp = new ScpClient(server, username, password);
                scp.Connect();
                scp.Upload(new DirectoryInfo(mPATH1), mPATH2); //将d:\\aaaa下的所有文件上传
                scp.Disconnect();
            }
            catch (Exception ee )
            {

                this.Dispatcher.Invoke(new Action(delegate
                {
                    k2++;
                    tb4.Text = k2 + "  " + ee.ToString();
                }));


                return;
            }
            this.Dispatcher.Invoke(new Action(delegate
            {
                k1++;
                tb4.Text = "Good " + k1;
            }));



        }
    }
}
